package com.golden.order02.service;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.order02.dao.OrderHisSalesMapper;
import com.golden.order02.domain.OrderHisSalesDO;
import com.golden.order02.domain.ResultDO;
import com.golden.order02.entity.OrderHisSales;
import com.golden.order02.query.OrderHisSalesExample;

@Service
public class OrderHisSalesServiceImpl implements OrderHisSalesService{

	@Autowired
	OrderHisSalesMapper orderHisSalesMapper;
	
	public ResultDO<OrderHisSalesDO> get(Integer id){
		ResultDO<OrderHisSalesDO> result=new ResultDO<OrderHisSalesDO>();
		BaseServiceImpl<OrderHisSalesDO,OrderHisSales> bi=new BaseServiceImpl<>();
		OrderHisSalesDO oo=new OrderHisSalesDO();
		
		try {
			OrderHisSales selectByPrimaryKey = orderHisSalesMapper.selectByPrimaryKey(id);
			OrderHisSalesDO t = bi.getT(selectByPrimaryKey, oo);
			result.setData(t);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<List<OrderHisSalesDO>> query( OrderHisSalesExample example){
		
		ResultDO<List<OrderHisSalesDO>> result=new ResultDO<List<OrderHisSalesDO>>();
		BaseServiceImpl<OrderHisSalesDO,OrderHisSales> bi=new BaseServiceImpl<>();
		OrderHisSalesDO oo=new OrderHisSalesDO();
		try {
			List<OrderHisSales> selectByExample = orderHisSalesMapper.selectByExample(example);
			List<OrderHisSalesDO> tList = bi.getTList(selectByExample, oo);
			result.setData(tList);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<OrderHisSalesDO> add( OrderHisSalesDO beanDO){
		ResultDO<OrderHisSalesDO> result=new ResultDO<OrderHisSalesDO>();
		OrderHisSales oi=new OrderHisSales();
		try {
			BeanUtils.copyProperties(oi, beanDO);
			int insert = orderHisSalesMapper.insert(oi);
			if(insert<=0) {
				throw new Exception();
			}
			beanDO.setId(oi.getId());
			result.setData(beanDO);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
			result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<Integer> delete( Integer id){
		
		ResultDO<Integer> result=new ResultDO<Integer>();
		try {
			int deleteByPrimaryKey = orderHisSalesMapper.deleteByPrimaryKey(id);
			if(deleteByPrimaryKey<=0) {
				throw new Exception();
			}
			result.setData(id);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_DELETE_ERROR);
			result.setErrorMsg(ResultDO.ORDER_DELETE_MSG_ERROR);
		}
		return result;
	}
	
	public ResultDO<OrderHisSalesDO> modify(OrderHisSalesDO beanDO){
		
		ResultDO<OrderHisSalesDO> result=new ResultDO<OrderHisSalesDO>();
		BaseServiceImpl<OrderHisSales, OrderHisSalesDO> bi=new BaseServiceImpl<>();
		OrderHisSales oi=new OrderHisSales();
		OrderHisSales t = bi.getT(beanDO, oi);
		try {
			int insert = orderHisSalesMapper.updateByPrimaryKeySelective(t);
			if(insert<=0) {
				throw new Exception();
			}
			result.setData(beanDO);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
			result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR);
		}
		return result;
	}
}
